package work.huangxin.share.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import work.huangxin.share.basemapper.SameService;
import work.huangxin.share.constant.RedisConstant;
import work.huangxin.share.mapper.ArticleMessageMapper;
import work.huangxin.share.domain.other.ArticleMessage;
import work.huangxin.share.util.RedisUtil;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ArticleOperationService extends SameService<ArticleMessage> {

    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private ArticleMessageMapper articleMessageMapper;
    //@Async
    //public Future<PageInfo> getNewArticle(Integer pageNumber, Integer articleId, Integer themeId, Integer userId) {
    public PageInfo getNewArticle(Integer pageNumber, Integer articleId, Integer themeId, Integer userId) {
        //先去redis中查找数据数据存入redis中
//        PageInfo pageResult = (PageInfo) redisUtil.get(RedisConstant.articlePage + pageNumber);
//        if (!Objects.isNull(pageResult)){
//            //如果数据不为空，则直接返回
//            return pageResult;
//        }
        PageHelper.startPage(pageNumber, 8);
        //数据为空，到mysql中查询
        PageInfo pageInfo = new PageInfo(articleMessageMapper.getNewArticle(articleId, themeId, userId));
        //将数据存入redis中,定时一分钟
//        redisUtil.set(RedisConstant.articlePage + pageNumber, pageInfo,60 + RedisConstant.getRandomSecond());
        //return  AsyncResult.forValue(pageInfo);
        return  pageInfo;
    }
    public PageInfo getNewArticle(Integer pageNumber, Integer articleId, Integer themeId, Integer userId,Integer campusId){
        PageHelper.startPage(pageNumber, 8);
        PageInfo<ArticleMessage> articleMessagePageInfo = PageInfo.of(articleMessageMapper.getNewArticle1(articleId, themeId, userId, campusId));
        return  articleMessagePageInfo;
    }
    @Autowired
//    @Qualifier("threadPoolExecutor")
    private ThreadPoolExecutor threadPoolExecutor;
    /**
     * 通过id获取文章
     *
     * @param articleId
     * @return
     */
    public ArticleMessage getNewArticleById(Integer articleId) {
        ArticleMessage  articleResult    = (ArticleMessage) redisUtil.get(RedisConstant.articleById + articleId);
        if (!Objects.isNull(articleResult)){
            // TODO 更新缓存到redis中
//            if (redisUtil.getExpire(RedisConstant.articleById + articleId) < 40){
//                //20秒内没有人访问这个帖子
//                articleMessageMapper.updateByPrimaryKey(articleResult);
//            }
            //重新设置60秒定时
            articleResult.setTotalView(articleResult.getTotalView() + 1);
            redisUtil.del(RedisConstant.articleById + articleId);
            //防止缓存雪崩
            redisUtil.set(RedisConstant.articleById+articleId, articleResult,600 + RedisConstant.getRandomSecond());
            //TODO 异步更新缓存到数据库中
            threadPoolExecutor.execute(()->{
                int i = articleMessageMapper.updateByPrimaryKey(articleResult);
                if (i <= 0){
                    throw new RuntimeException("浏览量更新失败");
                }
            });
            return articleResult;
        }
        ArticleMessage newArticleById = articleMessageMapper.getNewArticleById(articleId);
        redisUtil.set(RedisConstant.articleById+articleId, newArticleById,60);
        //设置浏览量
        newArticleById.setTotalView(newArticleById.getTotalView() + 1);
        articleMessageMapper.updateByPrimaryKey(newArticleById);
        return newArticleById;
    }

    /**
     * 获取精选文章
     */
//    @Async
    //public Future<PageInfo> getAllLikeArticle(Integer pageNumber) {
    public PageInfo getAllLikeArticle(Integer pageNumber,Integer articleId, Integer themeId, Integer userId) {

        PageHelper.startPage(pageNumber, 8);
        PageInfo pageInfo = PageInfo.of(articleMessageMapper.getAllLikeArticles(articleId,themeId,userId));
        //return AsyncResult.forValue(pageInfo);
        return pageInfo;
    }
    public PageInfo getAllLikeArticle(Integer pageNumber,Integer articleId, Integer themeId, Integer userId,Integer campusId) {

        PageHelper.startPage(pageNumber, 8);
        PageInfo pageInfo = PageInfo.of(articleMessageMapper.getAllLikeArticles1(articleId,themeId,userId,campusId));
        //return AsyncResult.forValue(pageInfo);
        return pageInfo;
    }


    public Integer getTotalArticle() {
        return articleMessageMapper.getTotalArticle();
    }
}
